

#include <iostream>
#include <sstream>
#include <string>
#include "common_util.h"
#include "logging.h"

// std::string hex_to(const uint8_t value) // 0x1A //0001 1010
// {
//     static const char HEX[] = "0123456789ABCDEF";
//     uint8_t high = static_cast<uint8_t>(value >> 4);
//     uint8_t low = static_cast<uint8_t>(value & 0x0F);
//     return {HEX[high], HEX[low]};
// }

// template <typename T>
//  std::string hexToStr(const T *hex, std::size_t len)
std::string hexToStr(const uint8_t *hex, std::size_t len)
{
    std::ostringstream oss;

    static const char HEX[] = "0123456789ABCDEF";
    //     uint8_t high = static_cast<uint8_t>(value >> 4);
    //     uint8_t low = static_cast<uint8_t>(value & 0x0F);
    //     return {HEX[high], HEX[low]};

    for (std::size_t i = 0; i < len; ++i)
    {

        uint8_t high = static_cast<uint8_t>(hex[i] >> 4);
        uint8_t low = static_cast<uint8_t>(hex[i] & 0x0F);
        oss << HEX[high] << HEX[low];
        // if (hex[i] > 0xf)
        //     oss << std::hex << static_cast<uint8_t>(hex[i]);
        // else
        // {
        //     oss << std::hex << static_cast<uint8_t>(0);
        //     oss << std::hex << static_cast<uint8_t>(hex[i]);
        // }
    }
    return oss.str();
}


void console_show_can_msg(canbus::CanFrame frame)
{
    uint8_t can_id_buf[4];
    can_id_buf[3] = static_cast<uint8_t>(frame.id);
    can_id_buf[2] = static_cast<uint8_t>(frame.id >> 8);
    can_id_buf[1] = static_cast<uint8_t>(frame.id >> 16);
    can_id_buf[0] = static_cast<uint8_t>(frame.id >> 24);
    std::string canID = hexToStr(can_id_buf, 4);
    std::string canData = hexToStr(frame.data, 8);
    std::string canRecv = canID + " " + canData;
    spdlog::info("canRecv :{} ", canRecv.c_str());
}
